{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GWEKvPCCxJke"
      },
      "source": [
        "##### Copyright 2020 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "l-m8KQ-nxK5l"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "O8FuVCLYxi_l"
      },
      "source": [
        "# TensorFlow Addons 画像 : 操作\n",
        "\n",
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td><a target=\"_blank\" href=\"https://www.tensorflow.org/addons/tutorials/image_ops\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\"> TensorFlow.orgで表示</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/ja/addons/tutorials/image_ops.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\"> Google Colab で実行</a></td>\n",
        "  <td><a target=\"_blank\" href=\"https://github.com/tensorflow/docs-l10n/blob/master/site/ja/addons/tutorials/image_ops.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\">GitHub でソースを表示{</a></td>\n",
        "      <td><a href=\"https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/ja/addons/tutorials/image_ops.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\">ノートブックをダウンロード/a0}</a></td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2a5ksOt-xsOl"
      },
      "source": [
        "## 概要\n",
        "\n",
        "このノートブックでは、TensorFlow Addons で画像操作を使用する方法をいくつか紹介します。\n",
        "\n",
        "この例で説明する画像操作のリストは以下の通りです。\n",
        "\n",
        "- `tfa.image.mean_filter2d`\n",
        "\n",
        "- `tfa.image.rotate`\n",
        "\n",
        "- `tfa.image.transform`\n",
        "\n",
        "- `tfa.image.random_hsv_in_yiq`\n",
        "\n",
        "- `tfa.image.adjust_hsv_in_yiq`\n",
        "\n",
        "- `tfa.image.dense_image_warp`\n",
        "\n",
        "- `tfa.image.euclidean_dist_transform`"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DMbjxr4PyMPF"
      },
      "source": [
        "# セットアップ"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "o_QTX_vHGbj7"
      },
      "outputs": [],
      "source": [
        "!pip install -U tensorflow-addons"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5hVIKCrhWh4a"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "import tensorflow_addons as tfa\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Q6Z2rsP8yp2v"
      },
      "source": [
        "# 画像を準備して検査する"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9gbgJP10z9WO"
      },
      "source": [
        "## 画像をダウンロードする"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IgUsVhBQ6dSg"
      },
      "outputs": [],
      "source": [
        "img_path = tf.keras.utils.get_file('tensorflow.png','https://tensorflow.org/images/tf_logo.png')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uheQOL-y0Fj3"
      },
      "source": [
        "## 画像を検査する"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MFGirRRZ0Y9k"
      },
      "source": [
        "### TensorFlow のアイコン"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "NRlvNQdm1YI8"
      },
      "outputs": [],
      "source": [
        "img_raw = tf.io.read_file(img_path)\n",
        "img = tf.io.decode_image(img_raw)\n",
        "img = tf.image.convert_image_dtype(img, tf.float32)\n",
        "img = tf.image.resize(img, [500,500])\n",
        "\n",
        "plt.title(\"TensorFlow Logo with shape {}\".format(img.shape))\n",
        "_ = plt.imshow(img)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "clXQrFVa2nN7"
      },
      "source": [
        "### 白黒バージョンを作成する"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "tbaIkUCS2eNv"
      },
      "outputs": [],
      "source": [
        "bw_img = 1.0 - tf.image.rgb_to_grayscale(img)\n",
        "\n",
        "plt.title(\"Mask image with shape {}\".format(bw_img.shape))\n",
        "_ = plt.imshow(bw_img[...,0], cmap='gray')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UwqfpOm--vV2"
      },
      "source": [
        "# tfa.image を使って遊ぶ"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jIa5HnomPds3"
      },
      "source": [
        "## 平均フィルタリング\n",
        "\n",
        "平均フィルタリングはフィルタリング技術の 1 つで、画像や信号のノイズ除去によく使用されます。この考え方は、画像をピクセル単位で処理し、隣接するピクセルの平均値で置き換えるというものです。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SutWnbRoHl6i"
      },
      "outputs": [],
      "source": [
        "mean = tfa.image.mean_filter2d(img, filter_shape=11)\n",
        "_ = plt.imshow(mean)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Mp6cU7I0-r2h"
      },
      "source": [
        "## 回転\n",
        "\n",
        "この操作は、特定の画像をユーザーが入力した角度（ラジアン単位）に回転させます。 "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9kxUES9sM8Jl"
      },
      "outputs": [],
      "source": [
        "rotate = tfa.image.rotate(img, tf.constant(np.pi/8))\n",
        "_ = plt.imshow(rotate)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WjMdSDKlBcPh"
      },
      "source": [
        "## 変換\n",
        "\n",
        "この操作は、特定の画像をユーザーが指定した変換ベクトルに基づいて変換します。 "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "HTh1Qpps8Rg5"
      },
      "outputs": [],
      "source": [
        "transform = tfa.image.transform(img, [1.0, 1.0, -250, 0.0, 1.0, 0.0, 0.0, 0.0])\n",
        "_ = plt.imshow(transform)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "O79BrK-bC8oh"
      },
      "source": [
        "## YIQ でランダムに HSV 変換する\n",
        "\n",
        "この操作は、特定の RGB 画像のカラースケールを YIQ に変更しますが、ここではデルタ色相と彩度の値を指定された範囲からランダムに選択します。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "zZBI-9XvBSuh"
      },
      "outputs": [],
      "source": [
        "delta = 0.5\n",
        "lower_saturation = 0.1\n",
        "upper_saturation = 0.9\n",
        "lower_value = 0.2\n",
        "upper_value = 0.8\n",
        "rand_hsvinyiq = tfa.image.random_hsv_in_yiq(img, delta, lower_saturation, upper_saturation, lower_value, upper_value)\n",
        "_ = plt.imshow(rand_hsvinyiq)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ruyvVnmCDBgj"
      },
      "source": [
        "## YIQ で HSV を調整する\n",
        "\n",
        "この操作は、特定の RGB 画像のカラースケールを YIQ に変更しますが、ここではランダムに選択するのではなく、デルタ色相と彩度の値はユーザーの入力値です。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vbCdwGtYChnQ"
      },
      "outputs": [],
      "source": [
        "delta = 0.5\n",
        "saturation = 0.3\n",
        "value = 0.6\n",
        "adj_hsvinyiq = tfa.image.adjust_hsv_in_yiq(img, delta, saturation, value)\n",
        "_ = plt.imshow(adj_hsvinyiq)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fdbCDYJkG8Gv"
      },
      "source": [
        "## 高密度画像ワープ\n",
        "\n",
        "この操作は、オフセットベクトルのフローフィールドで指定された任意の画像の非線形ワープを行います（例えば、ここではランダムな値を使用します）。 "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dG557eQDDtSK"
      },
      "outputs": [],
      "source": [
        "input_img = tf.image.convert_image_dtype(tf.expand_dims(img, 0), tf.dtypes.float32)\n",
        "\n",
        "flow_shape = [1, input_img.shape[1], input_img.shape[2], 2]\n",
        "init_flows = np.float32(np.random.normal(size=flow_shape) * 2.0)\n",
        "dense_img_warp = tfa.image.dense_image_warp(input_img, init_flows)\n",
        "dense_img_warp = tf.squeeze(dense_img_warp, 0)\n",
        "_ = plt.imshow(dense_img_warp)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FcLMnSKYPcjA"
      },
      "source": [
        "## ユークリッド距離変換\n",
        "\n",
        "この操作は、前景ピクセルから背景ピクセルまでのピクセル値をユークリッド距離で更新します。\n",
        "\n",
        "- 注意: これは二値化画像のみを受け取り、結果は変換された画像になります。異なる画像を指定した場合は、結果は単一の値の画像になります。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-OMh6oeRQaYQ"
      },
      "outputs": [],
      "source": [
        "gray = tf.image.convert_image_dtype(bw_img,tf.uint8)\n",
        "# The op expects a batch of images, so add a batch dimension\n",
        "gray = tf.expand_dims(gray, 0)\n",
        "eucid = tfa.image.euclidean_dist_transform(gray)\n",
        "eucid = tf.squeeze(eucid, (0, -1))\n",
        "_ = plt.imshow(eucid, cmap='gray')"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "image_ops.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
